package com.example.homework_exam

import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.AdapterView.OnItemClickListener
import android.widget.AdapterView.OnItemLongClickListener
import android.widget.ImageView
import android.widget.ListView
import android.widget.SearchView
import android.widget.Toast
import com.example.homework_exam.adapter.NotepadAdapter
import com.example.homework_exam.bean.NotepadBean
import com.example.homework_exam.database.SQLiteHelper
import com.example.homework_exam.utils.DBUtils.NOTEPAD_CONTENT
import com.example.homework_exam.utils.DBUtils.NOTEPAD_ID
import com.example.homework_exam.utils.DBUtils.NOTEPAD_TIME

class NotepadActivity : Activity() {
    var listView: ListView? = null
    var list: MutableList<NotepadBean>? = null
    var mSQLiteHelper: SQLiteHelper? = null
    var adapter: NotepadAdapter? = null
    var searchView: SearchView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_notepad)
        listView = findViewById<View>(R.id.listview) as ListView
        val add = findViewById<View>(R.id.add) as ImageView
        add.setOnClickListener {
            val intent = Intent(this@NotepadActivity, RecordActivity::class.java)
            startActivityForResult(intent, 1)
        }
        searchView = findViewById<View>(R.id.search_content) as SearchView
        searchView!!.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                // 当软键盘搜索按钮被点击时触发
                searchNotes(query)
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                // 当搜索框内容发生变化时触发
                searchNotes(newText)
                return true
            }
        })
        initData()
    }

    protected fun initData() {
        mSQLiteHelper = SQLiteHelper(this) //创建数据库
        showQueryData()
        listView!!.onItemClickListener = OnItemClickListener { parent, view, position, id ->
            val notepadBean: NotepadBean = list!![position]
            val intent = Intent(this@NotepadActivity, RecordActivity::class.java)
            intent.putExtra(NOTEPAD_ID, notepadBean.getId())
            intent.putExtra(NOTEPAD_TIME, notepadBean.getNotepadTime()) //记录的时间
            intent.putExtra(NOTEPAD_CONTENT, notepadBean.getNotepadContent()) //记录的内容
            this@NotepadActivity.startActivityForResult(intent, 1)
        }
        listView!!.onItemLongClickListener = OnItemLongClickListener { parent, view, position, id ->
            val dialog: AlertDialog
            val builder = AlertDialog.Builder(this@NotepadActivity)
                .setMessage(getString(R.string.confirm_delete))
                .setPositiveButton(getString(R.string.confirm)) { dialog, which ->
                    val notepadBean: NotepadBean = list!![position]
                    if (mSQLiteHelper!!.deleteData(notepadBean.getId())) {
                        list!!.removeAt(position)
                        adapter?.notifyDataSetChanged()
                        Toast.makeText(this@NotepadActivity, getString(R.string.delete_success), Toast.LENGTH_SHORT).show()
                    }
                }
                .setNegativeButton(getString(R.string.cancel)) { dialog, which -> dialog.dismiss() }
            dialog = builder.create()
            dialog.show()
            true
        }
    }

    private fun searchNotes(query: String?) {
        if (query.isNullOrBlank()) {
            showQueryData()
        } else {
            list = mSQLiteHelper?.searchNotes(query)
            adapter = NotepadAdapter(this, list)
            listView!!.adapter = adapter
        }
    }

    private fun showQueryData() {
        list = mSQLiteHelper?.query()
        adapter = NotepadAdapter(this, list)
        listView!!.adapter = adapter
    }

    public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1 && resultCode == 2 && data != null) {
            showQueryData()
        }
    }
}